package cn.community.care.module.system.service.status;

import cn.community.care.module.system.controller.admin.status.vo.StatusPageReqVO;
import cn.community.care.module.system.controller.admin.status.vo.StatusSaveReqVO;
import cn.community.care.module.system.dal.dataobject.basicinfo.BasicInfoDO;
import cn.community.care.module.system.dal.dataobject.status.StatusDO;
import cn.community.care.module.system.dal.mysql.basicinfo.BasicInfoMapper;
import cn.community.care.module.system.dal.mysql.status.StatusMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 健康状态 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class StatusServiceImpl implements StatusService {

    @Resource
    private StatusMapper statusMapper;

    @Resource
    private BasicInfoMapper basicInfoMapper;

    @Override
    public Long createStatus(StatusSaveReqVO createReqVO) {
        // 插入
        StatusDO status = BeanUtils.toBean(createReqVO, StatusDO.class);
        statusMapper.insert(status);
        // 返回
        return status.getId();
    }

    @Override
    public void updateStatus(StatusSaveReqVO updateReqVO) {
        // 校验存在
        validateStatusExists(updateReqVO.getId());
        // 更新
        StatusDO updateObj = BeanUtils.toBean(updateReqVO, StatusDO.class);
        statusMapper.updateById(updateObj);
    }

    @Override
    public void deleteStatus(Long id) {
        // 校验存在
        validateStatusExists(id);
        // 删除
        statusMapper.deleteById(id);
    }

    private void validateStatusExists(Long id) {
        if (statusMapper.selectById(id) == null) {
//            throw exception(STATUS_NOT_EXISTS);
        }
    }

    @Override
    public StatusDO getStatus(Long id) {
        return statusMapper.selectById(id);
    }

    @Override
    public PageResult<StatusPageReqVO> getStatusPage(StatusPageReqVO pageReqVO) {
        PageResult<StatusDO> statusDOPageResult = statusMapper.selectPage(pageReqVO);
        List<StatusDO> list = statusDOPageResult.getList();
        List<StatusPageReqVO> bean = BeanUtils.toBean(list, StatusPageReqVO.class);
        PageResult<StatusPageReqVO> pageResult = new PageResult<>();
        pageResult.setTotal(statusDOPageResult.getTotal());
        bean.forEach(statusDO -> {
            Long userId = statusDO.getUserId();
            BasicInfoDO basicInfoDO = basicInfoMapper.selectOne(BasicInfoDO::getUserId, userId);
            statusDO.setUserName(basicInfoDO.getName());
        });
        pageResult.setList(bean);
        pageResult.setList(bean);
        return pageResult;
    }

    @Override
    public StatusDO getStatusByUserID(Long id) {
        return statusMapper.selectOne(StatusDO::getUserId, id);
    }

}